// 引入模块
const express = require("express");
const bodyparser = require("body-parser");

// 创建服务器对象
const app = express();

// 注册中间件
// 静态资源
app.use(express.static("./www"));
// 解析body数据
app.use(bodyparser());

// let arr = [{username:"admin",password:123123}];
let userMsg = [];

// 路由处理
// 登录
app.get("/login",(req,res)=>{
    let i = 0;
    let onoff = userMsg.some((val,idx)=>{
        i = idx;
        return val.username === req.query.un
    });

    if(onoff){
        if(userMsg[i].password === req.query.pw){
            // 登录成功之后的操作
            // ...

            res.json({
                code:"1",
                msg:"登录成功"
            })
        }else{
            res.json({
                code:"2",
                msg:"用户名密码不相符"
            })
        }
    }else{
        res.json({
            code:"0",
            msg:"用户名不存在"
        })
    }
})
// 注册
app.post("/register",(req,res)=>{
    let onoff = userMsg.some(val=>val.username === req.body.un);
    if(onoff){
        res.json({
            code:"0",
            msg:"用户名重复"
        })
    }else{
        userMsg.push({
            username:req.body.un,
            password:req.body.pw
        })
        res.json({
            code:"1",
            msg:"注册成功"
        })
    }
})


// 监听端口
app.listen(3000);